package com.infore.airsupervise.ui.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infore.airsupervise.R;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.bean.AlarmQueryParamsE;
import com.infore.airsupervise.bean.StationAlarmE;
import com.infore.airsupervise.biz.StationAlarmQueryBiz;
import com.infore.airsupervise.biz.impl.StationAlarmQueryBizImpl;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.ui.widget.SwipeRecyclerView;
import com.infore.airsupervise.utils.JumpUtil;
import com.rey.material.widget.CheckBox;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * @features: 报警检索
 * @author: WuTao
 * @date: 2017/3/7 14:15
 */
public class AlarmSearchActivity
        extends BaseTitleBarActivity
        implements SwipeRecyclerView.OnActionListener {

    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.cb_all)
    CheckBox cbAll;
    @BindView(R.id.cb_first)
    CheckBox cbFirst;
    @BindView(R.id.cb_second)
    CheckBox cbSecond;
    @BindView(R.id.cb_three)
    CheckBox cbThree;
    @BindView(R.id.cb_four)
    CheckBox cbFour;
    @BindView(R.id.refreshRecyclerView)
    SwipeRecyclerView swipeRecyclerView;
    @BindView(R.id.tv_search)
    TextView tvSearch;

    private int isCheckedCount = 0;//复选框标识符
    private boolean isAll = true;//全部复选框标识符

    private BaseQuickAdapter<StationAlarmE, BaseViewHolder> quickAdapter;
    private List<StationAlarmE> alarmEList = new ArrayList<>();

    private int pageNum;
    private AlarmQueryParamsE queryParamsE;//请求参数
    private StationAlarmQueryBiz stationAlarmQueryBiz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_search);
        ButterKnife.bind(this);

        queryParamsE = new AlarmQueryParamsE();
        stationAlarmQueryBiz = new StationAlarmQueryBizImpl();
        initTitleBar();
        initCheckBox();
        initRecycleView();
    }

    private void initTitleBar() {
        title.setText("报警检索");
        rightMenu.setVisibility(View.INVISIBLE);
        tvSearch.setText("搜索");
    }

    /**
     * 设置列表和适配器
     */
    private void initRecycleView() {
        //适配器
        quickAdapter = new BaseQuickAdapter<StationAlarmE, BaseViewHolder>(R.layout.item_alarm, alarmEList) {
            @Override
            protected void convert(BaseViewHolder helper, StationAlarmE item) {
                helper.setText(R.id.tv_alarm_name, item.getStationName())
                        .setText(R.id.tv_alarm_content, item.getDiscrib())
                        .setText(R.id.tv_alarm_time, item.getTime());
                ImageView iv_icon = helper.getView(R.id.view_indicator);
                if (item.getWarningLevel() == null || item.getWarningLevel().isEmpty()) {
                    iv_icon.setImageResource(R.drawable.alarm_0);
                } else if (item.getWarningLevel().equals("一级")) {
                    iv_icon.setImageResource(R.drawable.alarm_1);
                } else if (item.getWarningLevel().equals("二级")) {
                    iv_icon.setImageResource(R.drawable.alarm_2);
                } else if (item.getWarningLevel().equals("三级")) {
                    iv_icon.setImageResource(R.drawable.alarm_3);
                } else if (item.getWarningLevel().equals("四级")) {
                    iv_icon.setImageResource(R.drawable.alarm_4);
                }
            }
        };

        // 设置recyclerView
        swipeRecyclerView.setAdapter(quickAdapter);
        swipeRecyclerView.setActionListener(this);
    }

    /**
     * 复选框
     */
    private void initCheckBox() {
        cbFirst.setOnCheckedChangeListener(changeListener);
        cbSecond.setOnCheckedChangeListener(changeListener);
        cbThree.setOnCheckedChangeListener(changeListener);
        cbFour.setOnCheckedChangeListener(changeListener);
        cbAll.setOnCheckedChangeListener(changeListener);
    }

    /**
     * 获取报警搜索信息
     */
    private void getQueryData(final boolean isRefresh) {
        queryParamsE.setPageNum(pageNum); //页码标识
        queryParamsE.setKeyWord(etSearch.getText().toString());//关键字
        queryParamsE.setType(2);//实时/历史
        queryParamsE.setWarningLevel(cbAll.isChecked() ? 1 : 0);
        queryParamsE.setWarningLevel_1(cbFirst.isChecked() ? 1 : 0);
        queryParamsE.setWarningLevel_2(cbSecond.isChecked() ? 1 : 0);
        queryParamsE.setWarningLevel_3(cbThree.isChecked() ? 1 : 0);
        queryParamsE.setWarningLevel_4(cbFour.isChecked() ? 1 : 0);
        queryParamsE.setUserId("1");

        Call call = stationAlarmQueryBiz.sendAlarmQueryRequest(
                queryParamsE, new OnServerResponseListener<List<StationAlarmE>>() {
                    @Override
                    public void onSucceed(List<StationAlarmE> result, String desc) {
                        swipeRecyclerView.commonSuccessHandle(isRefresh, result.size());
                        pageNum++;

                        if (isRefresh) {
                            alarmEList.clear();
                        }

                        alarmEList.addAll(result);
                        swipeRecyclerView.getAdapter().notifyDataSetChanged();
                    }

                    @Override
                    public void onFailed(int errorCode, String title, String desc) {
                        swipeRecyclerView.commonFailedHandle(isRefresh, errorCode, title, desc);
                    }
                });
        requests.add(call);
    }

    /**
     * 复选框选择事件
     */
    private CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            switch (compoundButton.getId()) {
                case R.id.cb_all://全部复选框
                    if (isAll || b) {
                        setCheckAll(b);
                    }
                    isAll = true;
                    break;
                default://其他复选框
                    isCheckedCount = b ? ++isCheckedCount : --isCheckedCount;
                    isAll = false;
                    cbAll.setChecked(isCheckedCount == 4 ? true : false);
                    break;
            }
        }
    };

    /**
     * 选择全部复选框 改变其他复选框状态的方法
     *
     * @param b
     */
    private void setCheckAll(boolean b) {
        cbFirst.setChecked(b);
        cbSecond.setChecked(b);
        cbThree.setChecked(b);
        cbFour.setChecked(b);
        isCheckedCount = b == false ? 0 : 4;
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        getQueryData(false);
    }

    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        pageNum = 1;
        getQueryData(true);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        JumpUtil.jumpFromTo(
                ctx, NewAQIActivity.class, Constants.INTENT_STR_KEY, alarmEList.get(position).getMn());
    }

    @OnClick({R.id.iv_searchClean, R.id.tv_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_searchClean:
                etSearch.getText().clear();
                break;
            case R.id.tv_search:
                swipeRecyclerView.getSwipeLayout().setRefreshing(true);
                swipeRecyclerView.onRefresh();
                break;
        }
    }
}
